#include "CBitmapFont.h"
#include "SGD Wrappers\CSGD_TextureManager.h"

CBitmapFont::CBitmapFont()
{
}

void CBitmapFont::SetBitmapInformation(int _nCharWidth, int _nCharHeight, int _nNumCols, char _cStartChar, int _nImageID)
{
	m_nCharWidth = _nCharWidth;
	m_nCharHeight = _nCharHeight;
	m_nNumCols = _nNumCols;
	m_cStartChar = _cStartChar;
	m_nImageID = _nImageID;
}

void CBitmapFont::Draw(const char* szTextToPrint, int nPosX, int nPosY, float fScale, DWORD dwColor)
{
	int nOriginX = nPosX;

	int len = strlen(szTextToPrint);

	for(int i = 0; i < len; ++i)
	{
		char c = szTextToPrint[i];
		if(c == ' ')
		{
			nPosX += int(m_nCharWidth*fScale);
			continue;
		}
		else if(c == '\n')
		{
			nPosX = nOriginX;
			nPosY += int(m_nCharHeight * fScale);
			continue;
				
		}
		int id = (int)(c - m_cStartChar);

		RECT rLetter = CellAlgorithm(id);

		CSGD_TextureManager::GetInstance()->Draw(m_nImageID, nPosX, nPosY, fScale, fScale, &rLetter, 0, 0, 0.0f, dwColor);
		nPosX += int(m_nCharWidth*fScale);
	}
}

RECT CBitmapFont::CellAlgorithm(int id)
{
	RECT rCell;
	rCell.left = (id % m_nNumCols) * m_nCharWidth;
	rCell.top = id / m_nNumCols * m_nCharHeight;
	rCell.right = rCell.left + m_nCharWidth;
	rCell.bottom = rCell.top + m_nCharHeight;

	return rCell;
}